package club.hicode.daydayup.threadlocal;

import cn.hutool.core.convert.Convert;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * xx
 *
 * @author Liu Chunfu
 * @date 2018-08-09 下午5:36
 **/
public class ThreadJoinTest {

    public static void main(String[] args) throws InterruptedException {

        List<Thread> threads = IntStream.range(1, 3).mapToObj(ThreadJoinTest::createThread)
                .collect(Collectors.toList());
        threads.forEach(Thread::start);

        for (Thread thread : threads) {
            //join可以理解为插队。就是
            thread.join();
        }

        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + "#" + i);
            shortSleep();
        }

    }

    private static Thread createThread(int seq) {
        return new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + "#" + i);
                shortSleep();
            }
        }, Convert.toStr(seq));
    }

    private static void shortSleep() {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
